var nearestPalindromic = function(n) {
    if(fangzhuang(n))return n 
    let left = n
    let right = n
    while(n){
        left = (Number(left)-1).toString()
        right = (Number(right)+1).toString()
        let p = fangzhuang(left)
        let q = fangzhuang(right)
        if(p){
            return left
        }
        if(q){
            return right
        }
    }
};

function fangzhuang(str1){
    let str2 = str1.split('').reverse().join('')
    return str1==str2
}

console.log(nearestPalindromic('123'));
